﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;

namespace VistaShades {
    /// <summary>
    /// Interaction logic for Settings.xaml
    /// </summary>
    public partial class ShadeSettings : Window {

        private VistaShades.Shade shade;
        private NotifyIcon ni = null;

        public ShadeSettings() {
            InitializeComponent();
            shade = new Shade();
            shade.Show();
            this.slider1.Value = shade.ShadeOpacity;

            this.Closing += new System.ComponentModel.CancelEventHandler(Settings_Closing);
            this.slider1.ValueChanged += new RoutedPropertyChangedEventHandler<double>(slider1_ValueChanged);

            //Using Windows Forms-based minimizing to system tray since that's not part of WPF.
            //Note that we have two sunglasses icon files.  The first is a "resource" which XAML wants for this form's icon, the second is
            //an "embedded resource" which is used for the system tray Notification Icon.
            ni = new System.Windows.Forms.NotifyIcon();
            ni.Icon = new System.Drawing.Icon(System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("VistaShades.sunglasses2.ico"));
            ni.Visible = true;
            ni.Click += new EventHandler(ni_Click); 
            
            //set the autostart checkbox
            this.checkAutoStart.IsChecked = AutoStart.IsAutoStartEnabled;            
            this.checkAutoStart.Checked += new RoutedEventHandler(checkAutoStart_Checked);
            this.checkAutoStart.Unchecked += new RoutedEventHandler(checkAutoStart_Unchecked);

            //if autostarted, minimize by default
            if (this.checkAutoStart.IsChecked == true) {
                this.WindowState = WindowState.Minimized;
                minimizeToTray();
            }
        }

        void checkAutoStart_Unchecked(object sender, RoutedEventArgs e) {            
            AutoStart.DisableAutoStart();  
        }

        void checkAutoStart_Checked(object sender, RoutedEventArgs e) { 
            AutoStart.EnableAutoStart();            
        }

        void ni_Click(object sender, EventArgs e) {
            this.WindowState = WindowState.Normal;
            this.Show();
            this.Topmost = true;
        }

        void minimizeToTray() {
            this.Hide();
            ni.Text = String.Format("Screen dimmed by {0}%.\nDouble-click to change.", this.slider1.Value); 
        }

        void slider1_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) {
            shade.ShadeOpacity = (int) e.NewValue;            
        }

        void Settings_Closing(object sender, System.ComponentModel.CancelEventArgs e) {
            if (shade != null) {
                ni.Dispose();
                ni = null;

                shade.Close();
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e) {
            minimizeToTray();
        }

        private void btnQuit_Click(object sender, RoutedEventArgs e) {
            this.Close();
        }

        private void btnHelp_Click(object sender, RoutedEventArgs e) {
            Process.Start("http://www.vistashades.com");
        }
    }
}
